//
//  NDEmptyContentView.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/11/1.
//

import SwiftUI

// MARK: - 空状态类型
enum NDEmptyState {
    case noData
    case noNetwork
    case finished
    case custom(icon: String, title: String, subtitle: String? = nil)
    
    var icon: String {
        switch self {
        case .noData: return "no_data_icon"
        case .noNetwork: return "no_data_icon"
        case .finished: return ""
        case .custom(let icon, _, _): return icon
        }
    }
    
    var title: String {
        switch self {
        case .noData: return localizedString("no_data_title")
        case .noNetwork: return localizedString("no_data_title")
        case .finished: return ""
        case .custom(_, let title, _): return title
        }
    }
    
    var subtitle: String? {
        switch self {
        case .noData: return localizedString("no_data_sub_title")
        case .noNetwork: return localizedString("no_data_sub_title")
        case .finished: return ""
        case .custom(_, _, let subtitle): return subtitle
        }
    }
}

// MARK: - 空状态配置协议
protocol NDEmptyStateConfigurable {
    var emptyState: NDEmptyState? { get }
    var emptyActionTitle: String? { get }
    var emptyAction: (() -> Void)? { get }
    var showEmptyImage: Bool { get }
}

// MARK: - 默认配置实现
extension NDEmptyStateConfigurable {
    var emptyActionTitle: String? { nil }
    var emptyAction: (() -> Void)? { nil }
    var showEmptyImage: Bool { true }
}

// MARK: - 空状态 ViewModifier
struct NDEmptyStateModifier: ViewModifier {
    let config: NDEmptyStateConfigurable
    
    func body(content: Content) -> some View {
        Group {
            if let emptyState = config.emptyState {
                NDEmptyContentView(
                    state: emptyState,
                    actionTitle: config.emptyActionTitle,
                    action: config.emptyAction,
                    showImage: config.showEmptyImage
                )
            } else {
                content
            }
        }
    }
}

// MARK: - 空状态内容视图
struct NDEmptyContentView: View {
    let state: NDEmptyState
    let actionTitle: String?
    let action: (() -> Void)?
    let showImage: Bool
    
    var body: some View {
        VStack(spacing: 20) {
            if showImage,
               !state.icon.isEmpty {
                Image(state.icon)
                    .resizable()
                    .scaledToFit()
                    .frame(width: 120, height: 120)
                    .foregroundColor(Color(hex: "#CCCCCC"))
            }
            
            VStack(spacing: 8) {
                Text(state.title)
                    .font(.system(size: 16, weight: .medium))
                    .foregroundColor(Color(hex: "#271F24"))
                    .multilineTextAlignment(.center)
                
                if let subtitle = state.subtitle {
                    Text(subtitle)
                        .font(.system(size: 12))
                        .foregroundColor(Color(hex: "#999999"))
                        .multilineTextAlignment(.center)
                        .lineSpacing(4)
                }
            }
            
            if let actionTitle = actionTitle, let action = action {
                Button(action: action) {
                    Text(actionTitle)
                        .font(.system(size: 16, weight: .semibold))
                        .foregroundColor(Color(hex: "#FFFFFF"))
                        .frame(height: 44)
                        .padding(.horizontal, 24)
                        .contentShape(Rectangle())
                        .background(Color(hex: "#FD632B"))
                        .clipShape(Capsule())
                }
                .padding(.horizontal, 24)
            }
        }
        .padding(.horizontal, 20)
        .padding(.vertical, 40)
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.clear)
    }
}

// MARK: - View Extension - 类似协议的使用方式
extension View {
    func ndEmptyState(_ config: NDEmptyStateConfigurable) -> some View {
        self.modifier(NDEmptyStateModifier(config: config))
    }
    
    func ndEmptyState(
        _ state: NDEmptyState?,
        actionTitle: String? = nil,
        action: (() -> Void)? = nil,
        showImage: Bool = true
    ) -> some View {
        self.modifier(NDEmptyStateModifier(config: SimpleEmptyStateConfig(
            emptyState: state,
            emptyActionTitle: actionTitle,
            emptyAction: action,
            showEmptyImage: showImage
        )))
    }
}

// MARK: - 简化配置结构体
struct SimpleEmptyStateConfig: NDEmptyStateConfigurable {
    let emptyState: NDEmptyState?
    let emptyActionTitle: String?
    let emptyAction: (() -> Void)?
    let showEmptyImage: Bool
    
    init(emptyState: NDEmptyState?,
         emptyActionTitle: String? = nil,
         emptyAction: (() -> Void)? = nil,
         showEmptyImage: Bool = true) {
        self.emptyState = emptyState
        self.emptyActionTitle = emptyActionTitle
        self.emptyAction = emptyAction
        self.showEmptyImage = showEmptyImage
    }
}

// MARK: - 列表专用空状态
struct ListEmptyStateConfig: NDEmptyStateConfigurable {
    let isEmpty: Bool
    let state: NDEmptyState
    let actionTitle: String?
    let action: (() -> Void)?
    
    var emptyState: NDEmptyState? {
        isEmpty ? state : nil
    }
    
    var emptyActionTitle: String? { actionTitle }
    var emptyAction: (() -> Void)? { action }
    var showEmptyImage: Bool { true }
    
    init(isEmpty: Bool,
         state: NDEmptyState,
         actionTitle: String? = nil,
         action: (() -> Void)? = nil) {
        self.isEmpty = isEmpty
        self.state = state
        self.actionTitle = actionTitle
        self.action = action
    }
}
